faApp.controller('SimuLsjzCtrl',['$scope','simuService','simuLsjzService','$location','$controller','$routeParams','$filter',function($scope,simuService,simuLsjzService,$location,$controller,$routeParams,$filter) {   
   
	$controller('BaseCtrl', {$scope: $scope}); 
  $scope.id = $routeParams.id;
  $scope.formData = {id:$scope.id};
  $scope.item = {};
  $scope.jj = {};
  $scope.list = [];

  $scope.loading = true; 
  simuService.query({id:$scope.id}).then(function(json){
    $scope.jj = json.detail;
    json.detail.lsjzJson = App.filterSimuJson(json.detail.lsjzJson || "[]");
    $scope.list = angular.fromJson(json.detail.lsjzJson);
    $scope.formData.jjbm = $scope.jj.jjbm;
    $scope.item.createBy= $scope.jj.createBy;
  }); 

  $scope.edit = function(v){
    $scope.itemIndex = v;
    $scope.item = $scope.list[v];
  };

  $scope.deleteItem = function(v){
    $scope.list.splice(v,1);
    if (v > $scope.itemIndex){
      $scope.itemIndex --;
    }
    $scope.save(true);
  };

  $scope.save = function(deleteFlag){
    if (!deleteFlag){
      var v = $scope.itemIndex;
      if (v == undefined){
        $scope.list.unshift($scope.item);
      }else{
        $scope.list[v] = $scope.item;
      }
    }
    
    $scope.jj.lsjzJson = angular.toJson($scope.list);
    $scope.formData.lsjzJson = angular.toJson($scope.list);
    simuLsjzService.edit($scope.formData).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.addAlert("操作成功");
        $scope.item = {};
        $scope.itemIndex = undefined;
      }
    });
  };
 
}]);
